const useTagsViewStore = defineStore("tags-view", {
  state: () => ({
    visitedViews: [
      {
        path: "/shouye/lbsy",
        name: "Lbsy",
        params: {},
        query: {},
        hash: "",
        fullPath: "/shouye/lbsy",
        matched: [
          {
            path: "/shouye",
            redirect: "noRedirect",
            name: "Shouye",
            meta: {
              title: "首页",
              icon: "dashboard",
              noCache: false,
              link: null,
            },
            props: { default: false },
            children: [
              {
                name: "Lbsy",
                path: "lbsy",
                hidden: false,
                component: {
                  _custom: {
                    type: "function",
                    display: '<span style="opacity:.5;">function</span> res()',
                    tooltip: "<pre>() => modules[path]()</pre>",
                    _reviveId: 0,
                  },
                },
                meta: {
                  title: "列表首页",
                  icon: "build",
                  noCache: false,
                  link: null,
                },
              },
            ],
            instances: { default: {} },
            leaveGuards: [],
            updateGuards: [],
            enterCallbacks: {},
            components: {
              default: {
                _custom: {
                  type: "component-definition",
                  display: "Layout",
                  tooltip: "Component definition",
                  file: "C:/Users/jssh/Desktop/70newweb/src/layout/index.vue",
                },
              },
            },
            __vd_id: "18",
          },
          {
            path: "/shouye/lbsy",
            name: "Lbsy",
            meta: {
              title: "列表首页",
              icon: "build",
              noCache: false,
              link: null,
            },
            props: { default: false },
            children: [],
            instances: { default: {} },
            leaveGuards: [],
            updateGuards: [],
            enterCallbacks: {},
            components: {
              default: {
                _custom: {
                  type: "component-definition",
                  display: "Liebiaoshouye",
                  tooltip: "Component definition",
                  file: "C:/Users/jssh/Desktop/70newweb/src/views/shouye/liebiaoshouye.vue",
                },
              },
            },
            __vd_id: "19",
          },
        ],
        meta: { title: "列表首页", icon: "build", noCache: false, link: null },
        redirectedFrom: {
          fullPath: "/shouye/lbsy",
          path: "/shouye/lbsy",
          query: {},
          hash: "",
          params: { pathMatch: ["shouye", "lbsy"] },
          matched: [
            {
              path: "/:pathMatch(.*)*",
              meta: {},
              props: { default: false },
              children: [],
              instances: {},
              leaveGuards: [],
              updateGuards: [],
              enterCallbacks: {},
              components: {
                default: {
                  _custom: {
                    type: "function",
                    display:
                      '<span style="opacity:.5;">function</span> component()',
                    tooltip:
                      '<pre>() => import("/src/views/error/404.vue")</pre>',
                    _reviveId: 1,
                  },
                },
              },
              __vd_id: "59",
            },
          ],
          meta: {},
          href: "/shouye/lbsy",
        },
        title: "列表首页",
      },
    ],
    cachedViews: [],
    iframeViews: [],
  }),
  actions: {
    addView(view) {
      this.addVisitedView(view);
      this.addCachedView(view);
    },
    addIframeView(view) {
      if (this.iframeViews.some((v) => v.path === view.path)) return;
      this.iframeViews.push(
        Object.assign({}, view, {
          title: view.meta.title || "no-name",
        })
      );
    },
    addVisitedView(view) {
      if (this.visitedViews.some((v) => v.path === view.path)) return;
      this.visitedViews.push(
        Object.assign({}, view, {
          title: view.meta.title || "no-name",
        })
      );
    },
    addCachedView(view) {
      if (this.cachedViews.includes(view.name)) return;
      if (!view.meta.noCache) {
        this.cachedViews.push(view.name);
      }
    },
    delView(view) {
      return new Promise((resolve) => {
        this.delVisitedView(view);
        this.delCachedView(view);
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews],
        });
      });
    },
    delVisitedView(view) {
      return new Promise((resolve) => {
        for (const [i, v] of this.visitedViews.entries()) {
          if (v.path === view.path) {
            this.visitedViews.splice(i, 1);
            break;
          }
        }
        this.iframeViews = this.iframeViews.filter(
          (item) => item.path !== view.path
        );
        resolve([...this.visitedViews]);
      });
    },
    delIframeView(view) {
      return new Promise((resolve) => {
        this.iframeViews = this.iframeViews.filter(
          (item) => item.path !== view.path
        );
        resolve([...this.iframeViews]);
      });
    },
    delCachedView(view) {
      return new Promise((resolve) => {
        const index = this.cachedViews.indexOf(view.name);
        index > -1 && this.cachedViews.splice(index, 1);
        resolve([...this.cachedViews]);
      });
    },
    delOthersViews(view) {
      return new Promise((resolve) => {
        this.delOthersVisitedViews(view);
        this.delOthersCachedViews(view);
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews],
        });
      });
    },
    delOthersVisitedViews(view) {
      return new Promise((resolve) => {
        this.visitedViews = this.visitedViews.filter((v) => {
          return v.meta.affix || v.path === view.path;
        });
        this.iframeViews = this.iframeViews.filter(
          (item) => item.path === view.path
        );
        resolve([...this.visitedViews]);
      });
    },
    delOthersCachedViews(view) {
      return new Promise((resolve) => {
        const index = this.cachedViews.indexOf(view.name);
        if (index > -1) {
          this.cachedViews = this.cachedViews.slice(index, index + 1);
        } else {
          this.cachedViews = [];
        }
        resolve([...this.cachedViews]);
      });
    },
    delAllViews(view) {
      return new Promise((resolve) => {
        this.delAllVisitedViews(view);
        this.delAllCachedViews(view);
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews],
        });
      });
    },
    delAllVisitedViews(view) {
      return new Promise((resolve) => {
        const affixTags = this.visitedViews.filter((tag) => tag.meta.affix);
        this.visitedViews = affixTags;
        this.iframeViews = [];
        resolve([...this.visitedViews]);
      });
    },
    delAllCachedViews(view) {
      return new Promise((resolve) => {
        this.cachedViews = [];
        resolve([...this.cachedViews]);
      });
    },
    updateVisitedView(view) {
      for (let v of this.visitedViews) {
        if (v.path === view.path) {
          v = Object.assign(v, view);
          break;
        }
      }
    },
    delRightTags(view) {
      return new Promise((resolve) => {
        const index = this.visitedViews.findIndex((v) => v.path === view.path);
        if (index === -1) {
          return;
        }
        this.visitedViews = this.visitedViews.filter((item, idx) => {
          if (idx <= index || (item.meta && item.meta.affix)) {
            return true;
          }
          const i = this.cachedViews.indexOf(item.name);
          if (i > -1) {
            this.cachedViews.splice(i, 1);
          }
          if (item.meta.link) {
            const fi = this.iframeViews.findIndex((v) => v.path === item.path);
            this.iframeViews.splice(fi, 1);
          }
          return false;
        });
        resolve([...this.visitedViews]);
      });
    },
    delLeftTags(view) {
      return new Promise((resolve) => {
        const index = this.visitedViews.findIndex((v) => v.path === view.path);
        if (index === -1) {
          return;
        }
        this.visitedViews = this.visitedViews.filter((item, idx) => {
          if (idx >= index || (item.meta && item.meta.affix)) {
            return true;
          }
          const i = this.cachedViews.indexOf(item.name);
          if (i > -1) {
            this.cachedViews.splice(i, 1);
          }
          if (item.meta.link) {
            const fi = this.iframeViews.findIndex((v) => v.path === item.path);
            this.iframeViews.splice(fi, 1);
          }
          return false;
        });
        resolve([...this.visitedViews]);
      });
    },
  },
});

export default useTagsViewStore;
